package org.zxp.datastructure.map;

import org.junit.Test;

import java.util.*;

/**
 * @program: effectiveJava
 * @description: TreeMap测试
 * @author: X-Pacific zhang
 * @create: 2019-07-26 11:26
 **/
public class TreeMapTest {
    public static void main(String[] args){
        TreeMap<Integer,String> tree = new TreeMap<>();
        tree.put(1, "qwewqe");
        tree.put(2, "qwewqe");
        tree.put(7, "qwewqe");
        tree.put(3, "qwewqe");
        tree.put(9, "qwewqe");
        tree.put(5, "qwewqe");
        tree.put(4, "qwewqe");
        tree.put(1, "1111");//treemap重复会覆盖value
        //treemap的有序是指key的有序而不是按照插入的时间顺序有序，这点要和linkedhashmap区分开来
        tree.forEach((k,v) -> System.out.println(k+"===="+v));
    }

    @Test
    public void testTreeMapGet(){
        TreeMap<Integer,String> tree = new TreeMap<>();
        long a1 = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            tree.put(i, UUID.randomUUID()+"");
        }
        long b1 = System.currentTimeMillis();
        System.out.println(b1-a1);//24150ms

        long a = System.nanoTime();
        System.out.println(tree.get(99999));
        long b = System.nanoTime();
        System.out.println(b-a);//97800ns
    }

    @Test
    public void testHashMapGet(){
        HashMap<Integer,String> map = new HashMap<>();
        long a1 = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            map.put(i, UUID.randomUUID()+"");
        }
        long b1 = System.currentTimeMillis();
        System.out.println(b1-a1);//27972ms

        long a = System.nanoTime();
        System.out.println(map.get(99999));
        long b = System.nanoTime();
        System.out.println(b-a);//83400ns
    }

    @Test
    public void testArrayList(){
        ArrayList arr= new ArrayList(10000000);
        long a1 = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            arr.add(i);
        }
        long b1 = System.currentTimeMillis();
        System.out.println(b1-a1);//2532ms

        long a = System.nanoTime();
        System.out.println(arr.get(99999));
        long b = System.nanoTime();
        System.out.println(b-a);//68000ns


        long a2 = System.nanoTime();
        System.out.println(arr.contains(99999));
        long b2 = System.nanoTime();
        System.out.println(b2-a2);//2989000ns
    }
}
